package com.ncloud.system.mapper;

import com.ncloud.system.entity.Menu;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * Created by shiqiang on 2017/7/28.
 */
@Mapper
public interface MenuMapper {
    @Select(value = { "select * from sys_menu where menu_id=#{0} and use_tag='1'" })
    @Results({ @Result(column = "menu_id", property = "menuId"), @Result(column = "menu_name", property = "menuName"),
            @Result(column = "parent_id", property = "parentId"), @Result(column = "menu_url", property = "menuUrl"),
            @Result(column = "menu_desc", property = "menuDesc"), @Result(column = "icon", property = "icon"),
            @Result(column = "use_tag", property = "useTag"), @Result(column = "sort", property = "sort"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    public Menu getOneMenu(Long menuId);

    @Insert("insert into sys_menu(menu_id,menu_name, parent_id, menu_url, menu_desc,icon,use_tag, sort, create_user, create_date, update_date, update_user, remarks)"
            + " values(#{menuId},#{menuName}, #{parentId}, #{menuUrl}, #{menuDesc},#{icon},#{useTag}, #{sort}, #{createUser}, #{createDate}, #{updateDate}, #{updateUser}, #{remarks})")
    public int insertMenu(Menu menu);

    @Update("update sys_menu set menu_name=#{menuName}, icon=#{icon}, parent_id=#{parentId}, menu_url=#{menuUrl},"
            + " sort=#{sort}, update_date=#{updateDate}, update_user=#{updateUser} where menu_id=#{menuId} and use_tag='1'")
    public int updateMenu(Menu menu);

    @Update("update sys_menu set use_tag=#{0} where menu_id=#{1}")
    public int updateMenuState(String useTag,Long menuId);

    @Update("update sys_menu set sort=#{1} where menu_id=#{0}")
    public int updateMenuSort(Long menuId,Integer sort);

    /* @Update("update sys_menu set sort=#{2} where parent_id=#{0} and sort= #{1} and use_tag='1'")
     public int updateMenuSort(Long parentId,Integer oldSort,Integer newSort);*/
    /*查询是否有同名菜单*/
    @Select(value = { "select count(1) as num from sys_menu  where  parent_id=#{0} and menu_name= #{1} " })
    int getSameMenuName(Long parentId,String menuName);

    /*查询是否有同名顺序*/
    @Select(value = { "select count(1) as num from sys_menu  where  parent_id=#{0} and sort= #{1} " })
    int getSameMenuOrder(Long parentId,Integer sort);
    /**/
    @Select(value = { "select * from sys_menu  where  parent_id=#{0} and sort= #{1} and menu_id <> 0 and use_tag='1'" })
    @Results({ @Result(column = "menu_id", property = "menuId"), @Result(column = "menu_name", property = "menuName"),
            @Result(column = "parent_id", property = "parentId"), @Result(column = "menu_url", property = "menuUrl"),
            @Result(column = "menu_desc", property = "menuDesc"), @Result(column = "icon", property = "icon"),
            @Result(column = "use_tag", property = "useTag"), @Result(column = "sort", property = "sort"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    List<Menu> getMenuBySort(Long parentId,Integer sort);

    /*根据角色查询已经付权的菜单*/
    @Select(value = { "SELECT DISTINCT a.* from sys_menu a ,sys_role b,sys_role_menu c where a.menu_id =c.menu_id and b.role_id =c.role_id " +
            " and a.use_tag='1' and b.use_tag='1' and a.menu_id <> 0 and b.role_id =#{0}" })
    @Results({ @Result(column = "menu_id", property = "menuId"), @Result(column = "menu_name", property = "menuName"),
            @Result(column = "parent_id", property = "parentId"), @Result(column = "menu_url", property = "menuUrl"),
            @Result(column = "menu_desc", property = "menuDesc"), @Result(column = "icon", property = "icon"),
            @Result(column = "use_tag", property = "useTag"), @Result(column = "sort", property = "sort"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    List<Menu> getMenusByRole(Long roleId);

    /*根据登录用户查询菜单*/
    @Select(value = { "SELECT DISTINCT b.*,e.MENU_NAME as parentName from sys_user a,sys_menu b ,sys_role c,sys_role_menu d,sys_menu e  where  " +
            " a.company_id=c.company_id and  b.parent_id = e.menu_id  and c.ROLE_ID=d.ROLE_ID and d.MENU_ID=b.MENU_ID and a.user_id=#{0} " +
            " and c.use_tag='1' and b.menu_id <> 0 order by  b.PARENT_ID ,b.sort" })
    @Results({ @Result(column = "menu_id", property = "menuId"), @Result(column = "menu_name", property = "menuName"),
            @Result(column = "parent_id", property = "parentId"), @Result(column = "menu_url", property = "menuUrl"),
            @Result(column = "menu_desc", property = "menuDesc"), @Result(column = "icon", property = "icon"),
            @Result(column = "use_tag", property = "useTag"), @Result(column = "sort", property = "sort"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    List<Menu> getMenusByUser(Long userId);

    /*根据登录用户查询菜单*/
    @Select(value = { "SELECT DISTINCT b.*,e.MENU_NAME as parentName from sys_menu b ,sys_menu e  where " +
            " b.parent_id = e.menu_id and  b.menu_id <> 0 order by  b.PARENT_ID ,b.sort" })
    @Results({ @Result(column = "menu_id", property = "menuId"), @Result(column = "menu_name", property = "menuName"),
            @Result(column = "parent_id", property = "parentId"), @Result(column = "menu_url", property = "menuUrl"),
            @Result(column = "menu_desc", property = "menuDesc"), @Result(column = "icon", property = "icon"),
            @Result(column = "use_tag", property = "useTag"), @Result(column = "sort", property = "sort"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    List<Menu> getMenusAll();
    /*删除菜单*/
    @Delete("delete from sys_menu where menu_id=#{0}")
    int deleteMenu(Long menuId);

    /*根据等级查询菜单*/
    @Select(value = { "select * from sys_menu where PARENT_ID=#{0} and menu_id <> 0 and use_tag='1'" })
    @Results({ @Result(column = "menu_id", property = "menuId"), @Result(column = "menu_name", property = "menuName"),
            @Result(column = "parent_id", property = "parentId"), @Result(column = "menu_url", property = "menuUrl"),
            @Result(column = "menu_desc", property = "menuDesc"), @Result(column = "icon", property = "icon"),
            @Result(column = "use_tag", property = "useTag"), @Result(column = "sort", property = "sort"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    List<Menu> getMenusByLevel(Long parentId);

}
